import {request} from "@/network/request.js"
import wx from "weixin-js-sdk"
var AppId = ''
var Timestamp = ''
var Signature = ''
var Noncestr = ''
function GetSignature (dataForWeixin,callback) {
  // qryWxSignature 这个是调用后台获取签名的接口
  request({
    url:"v1/share/get_config",
    data:{
      url:window.location.href.split('#')[0]
    }
  }).then((res) => {
      let data = res.data
    AppId = data.appId
    Timestamp = data.timestamp
    Signature = data.signature
    Noncestr = data.nonceStr
    wx.config({
      debug: false,
      appId: AppId,
      timestamp: Timestamp,
      nonceStr: Noncestr,
      signature: Signature,
      // 这里是把所有的方法都写出来了 如果只需要一个方法可以只写一个
      jsApiList: [
        'checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideMenuItems',
        'showMenuItems',
        'hideAllNonBaseMenuItem',
        'showAllNonBaseMenuItem',
        'translateVoice',
        'startRecord',
        'stopRecord',
        'onRecordEnd',
        'playVoice',
        'pauseVoice',
        'stopVoice',
        'uploadVoice',
        'downloadVoice',
        'chooseImage',
        'previewImage',
        'uploadImage',
        'downloadImage',
        'getNetworkType',
        'openLocation',
        'getLocation',
        'hideOptionMenu',
        'showOptionMenu',
        'closeWindow',
        'scanQRCode',
        'chooseWXPay',
        'openProductSpecificView',
        'addCard',
        'chooseCard',
        'openCard',
        'openWXDeviceLib',
        'closeWXDeviceLib',
        'configWXDeviceWiFi',
        'getWXDeviceInfos',
        'sendDataToWXDevice',
        'startScanWXDevice',
        'stopScanWXDevice',
        'connectWXDevice',
        'disconnectWXDevice',
        'getWXDeviceTicket',
        'WeixinJSBridgeReady',
        'onWXDeviceBindStateChange',
        'onWXDeviceStateChange',
        'onScanWXDeviceResult',
        'onReceiveDataFromWXDevice',
        'onWXDeviceBluetoothStateChange'
      ]
    })
    wx.ready(function () {
        wx.onMenuShareAppMessage({
            title: dataForWeixin.title,
            desc: dataForWeixin.desc,
            link: dataForWeixin.linkurl,
            imgUrl: dataForWeixin.img,
            // trigger: function trigger(res) {},
            success: function success(res) {
                console.log(dataForWeixin);
                console.log(dataForWeixin.img);
                console.log('已分享');
            },
            cancel: function cancel(res) {
                console.log('已取消');
            },
            fail: function fail(res) {
                console.log(res);
            }
        });
        // 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
        wx.onMenuShareTimeline({
            title: dataForWeixin.title,
            link: dataForWeixin.linkurl,
            imgUrl: dataForWeixin.img, 
            trigger: function trigger(res) {
                // alert('用户点击分享到朋友圈');
            },
            success: function success(res) {
                //alert('已分享');
            },
            cancel: function cancel(res) {
                //alert('已取消');
            },
            fail: function fail(res) {
                // alert(JSON.stringify(res));
            }
        });
      if (callback) callback()
    })
  })
}
export {
  GetSignature
}